const regeneratorRuntime = global.regeneratorRuntime = require('./libs/regenerator');
const co = require('./libs/co');
const passport = require('./services/passport/passport');

//app.js
App({
  profile: null,
  onLaunch: function (options) {
      this.getProfile();
      console.log();
  },
  onHide: function () {
      console.log('onHide');
  },

  getProfile: function (cb) {
      //授权
      var that = this;
      if (this.profile){
          cb(this.profile);
          return;
      }
      co(function *() {
          try{
              var profile = yield passport.wxLogin(true);
              console.log(profile);
              var ret = yield passport.authorize(profile);
              if (ret){
                  profile.bearerToken = ret.bearerToken;
                  that.profile = profile;
                  cb(profile);
              }else{
                  console.log("launch: authorize");
                  console.log(ret);
                  cb(false);
              }
          }catch(e){
              if (e.message == 1001){
                //   that.setData({
                //       tipText: "您必须授权后才能使用本小程序",
                //       showAuthorizeButton: true
                //   })
              }else{
                  console.log(e);
              }
          }
      });
  }

})
